#!/bin/sh
# LambdaNative - a cross-platform Scheme framework
# Copyright (c) 2009-2013, University of British Columbia
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the
# following conditions are met:
#
# * Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# * Neither the name of the University of British Columbia nor
# the names of its contributors may be used to endorse or
# promote products derived from this software without specific
# prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# lipo the payload

SYS_CPUs=`echo $SYS_CPUs | awk '{for (i=1;i<=NF;i++) if (!a[$i]++) printf("%s%s",$i,FS)}{printf("\n")}'`
payloads=
for SYS_CPU in $SYS_CPUs; do
  payloads="$payloads $SYS_PREFIXROOT/ios/$SYS_CPU/lib/libpayload.a"
done
ios_fatlib=$SYS_PREFIXROOT/ios/libpayload-fat.a
rmifexists $ios_fatlib
lipo $payloads -output $ios_fatlib -create
assertfile $ios_fatlib "lipo failed to create fat payload"
mv $ios_fatlib $SYS_PREFIX/lib/libpayload-fat.a

ios_subst()
{
  ac_subst IOS_OBJC_ADDITIONS "@$1/IOS_objc_additions"
  ac_subst IOS_OBJCPP_ADDITIONS "@$1/IOS_objcpp_additions"
  ac_subst IOS_OBJC_DEFINES "@$1/IOS_objc_defines"
  ac_subst IOS_FRAMEWORK_ADDITIONS "@$1/IOS_framework_additions"
  ac_subst IOS_FRAMEWORK_SEARCHPATH "@$1/IOS_framework_searchpath"
  ac_subst IOS_LAUNCHERAPPDELEGATE_ADDITIONS "@$1/IOS_launcherappdelegate_additions"
  ac_subst IOS_LAUNCHERAPPDELEGATE_DIDFINISHLAUNCHING "@$1/IOS_launcherappdelegate_didfinishlaunching"
  ac_subst IOS_XML_BACKGROUNDMODES "@$1/IOS_xml_backgroundmodes"
  ac_subst IOS_XML_REQUIREDDEVICECAP "@$1/IOS_xml_requireddevicecap"
  ac_subst IOS_XML_ADDITIONS "@$1/IOS_xml_additions"
}
ios_subst loaders/ios
for m in $modules; do
  modpath=`locatedir modules/$m silent`
  ios_subst $modpath
done
ios_subst `locatedir apps/$SYS_APPNAME`

tgtdir="$SYS_PREFIX/$SYS_APPNAME$SYS_APPFIX"
srcdir=`pwd`"/loaders/ios"
cmakedir=`mktemp -d $SYS_TMPDIR/tmp.XXXXXX`
ac_output $srcdir/CMakeLists.txt $cmakedir/CMakeLists.txt
ac_output $srcdir/objc_additions.m $cmakedir/objc_additions.m
ac_output $srcdir/objcpp_additions.mm $cmakedir/objcpp_additions.mm
ac_output $srcdir/mo_chooks.mm $cmakedir/mo_chooks.mm
ac_output $srcdir/launcherAppDelegate.m $cmakedir/launcherAppDelegate.m
cp $srcdir/*.m $cmakedir
cp $srcdir/*.mm $cmakedir
cp $srcdir/*.h $cmakedir

echo " => preparing icons.."
icondir="$cmakedir/Images.xcassets/AppIcon.appiconset"
mkdir -p "$icondir"
for file in `ls $SYS_PREFIXROOT/build/$SYS_APPNAME/artwork-*.png`; do 
  dfile=`basename $file`
  convert "$file" -background white -alpha off "$icondir/Icon${dfile:8:8}";
done
convert "$SYS_PREFIXROOT/build/$SYS_APPNAME/artwork-1024.png" -background white -alpha off "$icondir/AppIcon.png"
cp "$SYS_PREFIXROOT/build/$SYS_APPNAME/"Default*.png "$cmakedir"
cp $srcdir/Assets_Contents.json "$cmakedir/Images.xcassets/Contents.json"
cp $srcdir/Images_Contents.json "$icondir/Contents.json"
veval "xcrun actool $cmakedir/Images.xcassets --compile $cmakedir --platform iphoneos --minimum-deployment-target $SYS_IOSVERSION --app-icon AppIcon --output-partial-info-plist $cmakedir/partial.plist --output-format human-readable-text"
assertfile $cmakedir/Assets.car

#assetdir="$cmakedir/asset"
#mkdir -p $assetdir
#veval "xcrun actool $cmakedir/Images.xcassets --compile $assetdir --platform iphoneos --minimum-deployment-target $SYS_IOSVERSION --app-icon AppIcon --output-partial-info-plist $assetdir/partial.plist --output-format human-readable-text"
#assertfile $assetdir/Assets.car
#cp $assetdir/Assets.car $cmakedir

make_soundfile_ios()
{
  cp "$1" "$cmakedir"
}
make_sounds make_soundfile_ios

echo " => preparing plist.."
configsrc=loaders/ios/Info.plist.in
ac_output $configsrc $SYS_TMPDIR/tmp.xml
assertfile $SYS_TMPDIR/tmp.xml
if [ $SYS_MODE = release ]; then
  sed -i '' -e '/<key>UIFileSharingEnabled<\/key>/{' -e 'n;s/true/false/' -e '}' $SYS_TMPDIR/tmp.xml
fi
cat $SYS_TMPDIR/tmp.xml | sed '/^#/d' > "$SYS_PREFIXROOT/build/$SYS_APPNAME/Info.plist"

echo " => preparing custom config.."
headersrc=loaders/ios/config_custom.h.in
ac_output loaders/ios/config_custom.h.in "$cmakedir/config_custom.h"
assertfile "$cmakedir/config_custom.h"

echo " => building loader.."
xcodedir=`mktemp -d $SYS_TMPDIR/tmp.XXXXXX`
cd $xcodedir
veval "cmake -GXcode $cmakedir"
asserterror $?
#  xcodebuild -configuration Release | grep -A 2 error
veval "xcodebuild -configuration Release -allowProvisioningUpdates"
asserterror $?
cd $here
tmpappdir=$xcodedir/Release-iphoneos/$SYS_APPNAME$SYS_APPFIX
if [ ! -d $tmpappdir ]; then
  tmpappdir=$xcodedir/Release/$SYS_APPNAME$SYS_APPFIX
fi
if [ ! -d $tmpappdir ]; then
  tmpappdir=$xcodedir/Release-iphonesimulator/$SYS_APPNAME$SYS_APPFIX
fi
assertfile $tmpappdir

echo " => verifying application.."
osxver=`sw_vers -productVersion`
if [ `echo "10.9.5\n$osxver" | sed 's/^10\.//' | sort -g | head -1` == "9.5" ]; then
  checkfail=`codesign --verify --no-strict $tmpappdir 2>&1`
else
  checkfail=`codesign --verify $tmpappdir 2>&1`
fi
if [ "$checkfail" ]; then
  echo "ERROR: $SYS_APPNAME build was not successful [$checkfail]"
  exit 1
fi

echo " => relocating application.."
rmifexists "$tgtdir"
cp -R $tmpappdir $tgtdir

echo " => archiving application.."
configsrc=loaders/ios/App.xcscheme.in
ac_output $configsrc $SYS_TMPDIR/tmp.xcscheme
assertfile $SYS_TMPDIR/tmp.xcscheme
xcodedirbase=`basename $xcodedir`
sed -i '' "s/XPATH/$xcodedirbase/g" $SYS_TMPDIR/tmp.xcscheme
mkdir "$xcodedir/$SYS_APPNAME.xcodeproj/xcshareddata"
mkdir "$xcodedir/$SYS_APPNAME.xcodeproj/xcshareddata/xcschemes"
cat $SYS_TMPDIR/tmp.xcscheme | sed '/^#/d' > "$xcodedir/$SYS_APPNAME.xcodeproj/xcshareddata/xcschemes/$SYS_APPNAME.xcscheme"
cd $xcodedir
veval "xcodebuild -scheme $SYS_APPNAME archive -archivePath $SYS_PREFIX/$SYS_APPNAME-$SYS_APPVERSION.xcarchive -allowProvisioningUpdates"
cd $here
assertfile $SYS_PREFIX/$SYS_APPNAME-$SYS_APPVERSION.xcarchive

echo " => cleaning up.."
rm -rf "$xcodedir" 2> /dev/null
if [ $SYS_MODE = "debug" ]; then
  echo "DEBUG: ios build is in $cmakedir"
else
  rm -rf "$cmakedir" 2> /dev/null
fi

#eof
